package com.chazz.servlet;

import com.chazz.Do.GoodsDetailsInfo;
import com.chazz.Do.GoodsInfo;
import com.chazz.entity.GoodsSku;
import com.chazz.service.CfgoodsService;
import com.chazz.service.impl.CfgoodsServiceImpl;

import com.chazz.service.impl.GoodsInfoServiceImpl;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/cfgoods.do")
public class CfgoodsServlet extends HttpServlet {
    private static CfgoodsService cfgoodsService=new CfgoodsServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer catrgoryid=Integer.parseInt(req.getParameter("categoryid"));

        List<GoodsSku> goodsSkuList= null;
        List<GoodsDetailsInfo> goodsInfos = new ArrayList<>();
        try {
            goodsSkuList = cfgoodsService.findGoodsSkuBycategoryid(catrgoryid);
            for (GoodsSku goodsSku : goodsSkuList) {
                GoodsDetailsInfo goodsDetailsInfo = new GoodsInfoServiceImpl()
                    .sutureCommodityInformation(goodsSku.getGoods().getGoodsid());
                goodsInfos.add(goodsDetailsInfo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println(goodsSkuList);
        HttpSession session=req.getSession();

//        session.setAttribute("goodsSkuList",goodsSkuList);

        req.setAttribute("goodsSkuList",goodsInfos);
//        resp.sendRedirect("products.jsp");

        req.getRequestDispatcher("/products.jsp").forward(req,resp);
    }
}
